<!DOCTYPE html >

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <script type="text/javascript">

        // 方法: window.external.CallParentFunc 
        // 作用: 调用父窗口中的js函数, 目前只支持传递一个参数. 
        var names = window.external.CallParentFunc("getBkNames", "");
        var conts = window.external.CallParentFunc("getBkConts", "");
        var bkmkArr = names.split(";");
        var bkContArr = conts.split(";");
        var addBkmkArr = new Array(0);
        var addBkmkContArr = new Array(0);

        //首次加载数据
        function load() {
            pcheckBks();
            searchBookMark('');
            searchBookMark2('');
            return;
        }

        //遍历Word文件中已存在的书签名称及其对应的文本内容
        function pcheckBks() {
            var pBkNames = window.external.CallParentFunc("checkBkNames", "");
            var pBkConts = window.external.CallParentFunc("checkBkConts", "");

            if (pBkNames != "" && pBkNames != null && pBkConts != "" && pBkConts != null) {
                var currBkmArr = pBkNames.split(",");
                var currBkContArr = pBkConts.split(",");
                var start = 0;
                addBkmkArr = new Array(0);
                addBkmkContArr = new Array(0);

                for (var i = 0; i < currBkmArr.length; i++) {
                    if (currBkmArr[i] != "") {
                        //alert(currBkmArr[i]);
                        addBkmkArr.splice(0, 0, currBkmArr[i]); //添加
                        addBkmkContArr.splice(0, 0, currBkContArr[i]); //添加
                        for (var j = 0; j < bkmkArr.length; j++) {
                            if (bkmkArr[j] == currBkmArr[i]) {
                                start = j;
                            }
                        }
                        bkmkArr.splice(start, 1); //删除
                        bkContArr.splice(start, 1); //删除
                    }
                }
            }
        }

        //加载左侧数据列表
        function searchBookMark(s) {
            //删除所有行
            var tb1 = document.getElementById("bkmkTable");
            var rCount = tb1.rows.length;
            for (var i = 0; i < rCount; i++) {
                tb1.deleteRow(0);
            }

            var oTable = document.getElementById("bkmkTable");
            for (var i = 0; i < bkmkArr.length; i++) {
                if (bkmkArr[i] != null && bkmkArr[i] != "" && 0 == bkmkArr[i].toLocaleLowerCase().indexOf(s.toLocaleLowerCase())) {
                    var oTr = oTable.insertRow();
                    var oTd = oTr.insertCell();
                    oTd.innerHTML = bkmkArr[i];
                    oTd = oTr.insertCell();
                    oTd.innerHTML = bkContArr[i];
                    oTd = oTr.insertCell();
                    oTd.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick= \"add('" + bkmkArr[i] + "','" + bkContArr[i] + "')\">&nbsp;添加</a>";
                }
            }
        }

        //加载右侧数据列表
        function searchBookMark2(s) {
            //删除所有行
            var tb1 = document.getElementById("bkmkTable2");
            var rCount = tb1.rows.length;
            for (var i = 0; i < rCount; i++) {
                tb1.deleteRow(0);
            }

            var oTable = document.getElementById("bkmkTable2");
            if (addBkmkArr != null) {
                for (var i = 0; i < addBkmkArr.length; i++) {
                    if (addBkmkArr[i] != null && 0 == addBkmkArr[i].toLocaleLowerCase().indexOf(s.toLocaleLowerCase()) && "" != addBkmkArr[i]) {
                        var oTr = oTable.insertRow();
                        var oTd = oTr.insertCell();
                        oTd.innerHTML = addBkmkArr[i];
                        oTd = oTr.insertCell();
                        oTd.innerHTML = addBkmkContArr[i];
                        oTd = oTr.insertCell();
                        oTd.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick= \"locate('PO_" + addBkmkArr[i] + "')\">&nbsp;定位</a>";
                        oTd = oTr.insertCell();
                        oTd.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick= \"del('" + addBkmkArr[i] + "','" + addBkmkContArr[i] + "')\">&nbsp;删除</a>";
                    }
                }
            }
        }

        function Button1_onclick() {

            var s = document.getElementById("Text1").value.toLocaleLowerCase();
            var tb1 = document.getElementById("bkmkTable");
            var rCount = tb1.rows.length;
            for (var i = 0; i < rCount; i++) {
                tb1.deleteRow(0);
            }

            var oTable = document.getElementById("bkmkTable");
            for (var i = 0; i < bkmkArr.length; i++) {
                if (bkmkArr[i] != null && bkmkArr[i] != "" && bkmkArr[i].toLocaleLowerCase().indexOf(s) >= 0) {

                    var oTr = oTable.insertRow();
                    var oTd = oTr.insertCell();
                    oTd.innerHTML = bkmkArr[i];
                    oTd = oTr.insertCell();
                    oTd.innerHTML = bkContArr[i];
                    oTd = oTr.insertCell();
                    oTd.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick= \"add('" + bkmkArr[i] + "','" + bkContArr[i] + "')\">&nbsp;添加</a>";
                }
            }
        }

        function Button2_onclick() {
            var s = document.getElementById("Text2").value.toLocaleLowerCase();
            var tb1 = document.getElementById("bkmkTable2");
            var rCount = tb1.rows.length;
            for (var i = 0; i < rCount; i++) {
                tb1.deleteRow(0);
            }

            var oTable = document.getElementById("bkmkTable2");
            if (addBkmkArr != null) {
                for (var i = 0; i < addBkmkArr.length; i++) {

                    if (addBkmkArr[i] != null && addBkmkArr[i].toLocaleLowerCase().indexOf(s) >= 0 && "" != addBkmkArr[i]) {
                        var oTr = oTable.insertRow();
                        var oTd = oTr.insertCell();
                        oTd.innerHTML = addBkmkArr[i];
                        oTd = oTr.insertCell();
                        oTd.innerHTML = addBkmkContArr[i];
                        oTd = oTr.insertCell();
                        oTd.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick= \"locate('PO_" + addBkmkArr[i] + "')\">&nbsp;定位</a>";
                        oTd = oTr.insertCell();
                        oTd.innerHTML = "&nbsp;&nbsp;<a href=\"javascript:void(0);\"  onclick= \"del('" + addBkmkArr[i] + "','" + addBkmkContArr[i] + "')\">&nbsp;删除</a>";
                    }

                }
            }
        }

        //******** 书签操作 ************************************************************

        function add(name, content) {
            if ("true" == window.external.CallParentFunc("addBookMark", "PO_" + name + "=" + content)) {
                var start = 0;
                for (var i = 0; i < bkmkArr.length; i++) {
                    if (bkmkArr[i] == name) {
                        start = i;
                    }
                }

                addBkmkArr.splice(0, 0, name); //向数组第一个位置（0坐标处）添加一个元素
                addBkmkContArr.splice(0, 0, content);
                bkmkArr.splice(start, 1); //删除数组中相应坐标处的一个元素
                bkContArr.splice(start, 1);

                searchBookMark('');
                searchBookMark2('');
            }
        }

        function del(name, cont) {
            if ("true" == window.external.CallParentFunc("delBookMark", "PO_" + name)) {
                var start = 0;
                for (var i = 0; i < addBkmkArr.length; i++) {
                    if (addBkmkArr[i] == name) {
                        start = i;
                    }
                }

                bkmkArr.splice(0, 0, name);
                bkContArr.splice(0, 0, cont);
                addBkmkArr.splice(start, 1);
                addBkmkContArr.splice(start, 1);

                searchBookMark('');
                searchBookMark2('');
            }
            else {
                alert(0)
            }
        }

        function locate(bkName) {
            window.external.CallParentFunc("locateBK", bkName);
        }

    </script>
</head>
<body>
<div style="width: 470px; height: 320px;">
    <div style="float: left;font-size:12px;">
        <span>待添加标签：</span><br/>
        <input id="Text1" type="text" onpropertychange="searchBookMark(this.value);"
               oninput="searchBookMark(this.value);"/>
        <input id="Button1" type="button" value="搜索" onclick="return Button1_onclick()"/>
        <div style="width: 230px; height: 300px; border: solid 1px #ccc; overflow-y: scroll; ">
            <table id="bkmkTable" style=" font-size:12px;">
            </table>
        </div>
    </div>
    <div style="float: right;font-size:12px;">
        <span>已添加标签：</span><br/>
        <input id="Text2" type="text" onpropertychange="searchBookMark2(this.value);"
               oninput="searchBookMark2(this.value);"/>
        <input id="Button2" type="button" value="搜索" onclick="return Button2_onclick()"/>
        <div style="width: 230px; height: 300px; border: solid 1px #ccc; overflow-y: scroll; ">
            <table id="bkmkTable2" style=" font-size:12px;">
            </table>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    load();
    //alert(2);
</script>
</html>
